<?php
/**
 * 《加班信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/Plan.class.php');

class Overtime extends Plan {
	public $tbl = Constant::tbl_kq_overtime;

	public $overtime = array(
		'-' => '请选择',
		'1' => '普通加班',
		'2' => '节日加班',
		'3' => '调休加班',
		'4' => '自愿加班',
			
		'9' => '其它',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '等待排班',
		'3' => '正在排班',
		'4' => '已经排班',
		'5' => '未批准',
			
		'9' => '其它',
	);

	public $groupby = array(
		'-' => '请选择',
		'dept' => '部门',
		'emp' => '员工',
		'overtime' => '类型',
			
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('overtime_option', $this->overtime);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '员工',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		$controller->display($request, 'kq/overtime/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$overtime_s = $request['overtime_s'];

		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
		
		$state_s = $request['state_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($overtime_s) && $overtime_s <> '-') {
			$where .= " AND overtime = '" . $overtime_s . "'";
		}
		
		if (!empty($state_s) && $state_s <> '-') {
			$where .= " AND state = '" . $state_s . "'";
		}

		if (!empty($date1_s)) {
			$where .= " AND t1 >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND t2 < '" . $date2_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);				

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'kq/overtime/list');
		return true;
	}

	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'kq/overtime/list');
		return true;
	}

	function emp($controller, $request) {
		$this->pretreat($controller);

		$emp_s = $request['id'];

		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND t1 >= '%s' AND t2 <= '%s' ORDER BY t1";
		$sql = sprintf($fmt, $this->tbl, $emp_s, $date1_s, $date2_s);		debug($sql);
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row['emp_t'] = Name::emp($row['emp']);
			$row['overtime_t'] = $this->overtime[$row['overtime']];

			$row['week'] = $this->week($row['t1']);
			$row['date'] = $this->_date($row['t1'], $row['t2']);
			$row['tt_t'] = $this->_time_t($row['tt']);

			$row['status_t'] = $this->status[$row['status']];

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'kq/overtime/emp');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'kq/overtime/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$name = addslashes($request['name']);
		$overtime = $request['overtime'];

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$tt = $this->_tt($t1, $t2);

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, name, overtime, t1, t2, tt, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $name, $overtime, $t1, $t2, $tt, $content, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
		
		$this->findPost($controller, $request);

		return true;
	}
	
	function batch($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'kq/overtime/batch');
		return true;
	}
	
	function batchPost($controller, $request) {
		$this->pretreat($controller);
		
		$overtime = $request['overtime'];

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$tt = $this->_tt($t1, $t2);

		$content2 = addslashes($request['content']);

		$db = newdb($this->dsn_w);		
		$fmt = "INSERT INTO %s(office, dept, emp, title, overtime, t1, t2, tt, content, oper, status, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '2', now(), now())";
		
		$count = 0;
		
		$data = json_decode($request['item'], true);
		while (list($k, $v) = each($data)) {
			if (!$this->_ok($v['ok'])) {
				continue;
			}
			
			$emp = $this->_input($v['emp']);
			$office = $this->_office($emp);
			$dept = $this->_emp_dept($emp);
		
			$title = addslashes($v['title']);
			$content = addslashes($v['content']) . "\n" . $content2;
			
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $title, $overtime, $t1, $t2, $tt, $content, $_SESSION['id']);
			debug($sql);
			$db->query($sql);
			if ($db->affected_rows() > 0) {
				$count++;
			}
		}

		$this->_ret_json($count);
		return true;
	}

	/**
	 * 审核
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verify($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'kq/overtime/verify');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verifyPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'kq/overtime/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$name = addslashes($request['name']);
		$overtime = $request['overtime'];

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$tt = $this->_tt($t1, $t2);

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', name = '%s', overtime = '%s', t1 = '%s', t2 = '%s', tt = '%s', content = '%s'";
		$fmt .= ", modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $name, $overtime, $t1, $t2, $tt, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'kq/overtime/copy');
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'kq/overtime/upload');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		$sql = "DELETE FROM " . $this->tbl . " WHERE id " . $insql;
		// debug($sql);
		$query = $db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 1) {
			return false;
		}

		if ($this->_audit('111')) {
			return true;
		}

		if (strtotime($row['t1']) < strtotime("-8 hour")) {
			return false;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$row['files'] = $this->_files($row['id']);
		
		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('record', $this->_plan_record($row));

		$controller->display($request, 'kq/overtime/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}

		$row['week'] = $this->week($row['t1']);
		$row['date'] = $this->_date($row['t1'], $row['t2']);
		$row['tt_t'] = $this->_time_t($row['tt']);
		
		$row['overtime_t'] = $this->overtime[$row['overtime']];
		$row['state_t'] = $this->state[$row['state']];
		
		$row['files'] = $this->_files($row['id']);
		
		$row = $this->_view_t($row);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		$controller->assign('t2', strtotime('+1 day'));
		
		$controller->assign('result_s', "300");

		$num = array(
			'1' => '按次数',
			'2' => '按工时',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'kq/overtime/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'overtime');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(tt) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(tt) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);
		
		$where = $this->_where($request);
		
		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);
		
		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'overtime') {
			$row['a'] = $this->overtime[$row['a']];
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function t($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		// $fmt = "SELECT * FROM %s WHERE status = '2' AND t1 >= '%s' AND t2 < '%s'";
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$tt = $this->_tt($row['t1'], $row['t2']);

			$fmt = "UPDATE %s SET tt = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $tt, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function tj_daily($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "html4";

		$from = $this->email;
		$to = 'sunxy@tecmold.com';

		$subject = '申请加班情况';

		$this->result_s = 20;
		$this->groupby_s = 'emp';

		$body = $this->_tj_a($controller, $request);

		$this->mail_html($from, $to, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function _tj_a($controller, $request) {	
		$s = $this->groupby($controller, $this->groupby_s);
		$df = "DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')";

		$fmt = "SELECT %s as a, COUNT(%s) as c FROM %s a WHERE office = 1 AND t1 > %s GROUP BY %s ORDER BY c DESC";
		$sql = sprintf($fmt, $s, $s, $this->tbl, $df, $s);
		echo $sql . "\n";

		$this->_group_tj($controller, $sql);

		return $controller->sunxy($request, 'tj_a');
	}
	
	function ns($controller, $request) {

		$dd = date("Y-m-d");
		
		require_once(dirname(__FILE__) . '/Holiday.class.php');
		$aa = new Holiday();
		
		if ($aa->_is_holiday($dd)) {
			return false;
		}
		
		$from = 'caihw@tecmold.com';
		$subject = sprintf("%s加班人员名单", date("Y-m-d"));
		
		$data = $this->_ns();
		
		$controller->assign_by_ref('result', $data);	
		$body = $this->_tj_html($controller, $request, 'kq/overtime/ns');
		// echo $body;

		while (list($k, $v) = each($data['emp'])) {
			$to = $this->_emp_email($v['emp']);
			// $to = "sunxy@tecmold.com";
			
			$this->mail_html($from, $to, $subject, $body);
		}
		
		$bb = $this->_list_email('338');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$this->_tj_mail($from, $bb, $subject, $body);
	}
		
	function _ns() {
		$time = "DATE_FORMAT(now(), '%Y-%m-%d 19:00')";
		// $time = "DATE_FORMAT('2010-11-09', '%Y-%m-%d 19:00')";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE office = '1' AND t1 < %s AND t2 > %s";
		$sql = sprintf($fmt, $this->tbl, $time, $time);
		echo $sql . "\n";
		$result = $db->query($sql);
		$ret = array("male"=>0, "female"=>0);
		
		$emp = array();
		while ($row = $db->fetch_array($result)) {			
			// $row['emp_t'] = Name::emp($row['emp']);
			
			$a = Common::id(Constant::tbl_emp, $row['emp']);
			$row['emp_t'] = $a['name'];
			if ($a['sex'] == '1') {
				$ret['male']++;
			}
			
			if ($a['sex'] == '2') {
				$ret['female']++;
			}
						
			$emp[] = $row;
		}
		
		$ret['emp'] = $emp;
				
		return $ret;
	}
}
?>